package com.yanhua.transaction.transactional;

import com.alibaba.fastjson.JSON;
import com.yanhua.transaction.common.dto.TransactionGroupDto;
import com.yanhua.transaction.common.util.TransactionConst;
import com.yanhua.transaction.netty.NettyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class YhTransactionManager {


    private static NettyClient nettyClient;

    private static ThreadLocal<YhTransaction> current = new ThreadLocal<>();
    private static ThreadLocal<String> currentGroupId = new ThreadLocal<>();
    private static ThreadLocal<Integer> transactionCount = new ThreadLocal<>();

    @Autowired
    public void setNettyClient(NettyClient nettyClient) {
        YhTransactionManager.nettyClient = nettyClient;
    }

    public static Map<String, YhTransaction> LB_TRANSACION_MAP = new HashMap<>();

    /**
     * 创建事务组，并且返回groupId
     *
     * @return
     */
    public static String createYhTransactionGroup() {
        String groupId = UUID.randomUUID().toString();
        TransactionGroupDto build = TransactionGroupDto.builder()
                .groupId(groupId)
                .command(TransactionConst.TransactionCommand.create)
                .remarks("创建事务组")
                .build();
        nettyClient.send(JSON.toJSONString(build));
        System.out.println("创建事务组");
        currentGroupId.set(groupId);
        return groupId;
    }

    /**
     * 创建子事务
     *
     * @param groupId
     * @return
     */
    public static YhTransaction createYhTransaction(String groupId) {
        String transactionId = UUID.randomUUID().toString();
        YhTransaction yhTransaction = new YhTransaction(groupId, transactionId);
        LB_TRANSACION_MAP.put(groupId, yhTransaction);
        current.set(yhTransaction);
        addTransactionCount();
        System.out.println("创建事务");
        return yhTransaction;
    }

    /**
     * 添加事务
     *
     * @param yhTransaction
     * @param state
     * @param transactionType
     * @return
     */
    public static YhTransaction addYhTransaction(YhTransaction yhTransaction,
                                                 TransactionConst.TransactionState state,
                                                 TransactionConst.TransactionType transactionType) {
        TransactionGroupDto build = TransactionGroupDto.builder()
                .groupId(yhTransaction.getGroupId())
                .transactionId(yhTransaction.getTransactionId())
                .transactionType(transactionType)
                .command(TransactionConst.TransactionCommand.add)
                .state(state)
                .transactionCount(YhTransactionManager.getTransactionCount())
                .remarks("添加事务")
                .build();
        nettyClient.send(JSON.toJSONString(build));
        System.out.println("添加事务");
        return yhTransaction;
    }

    public static YhTransaction getLbTransaction(String groupId) {
        return LB_TRANSACION_MAP.get(groupId);
    }

    public static YhTransaction getCurrent() {
        return current.get();
    }

    public static String getCurrentGroupId() {
        return currentGroupId.get();
    }

    public static void setCurrentGroupId(String groupId) {
        currentGroupId.set(groupId);
    }

    public static Integer getTransactionCount() {
        return transactionCount.get();
    }

    public static void setTransactionCount(int i) {
        transactionCount.set(i);
    }

    public static Integer addTransactionCount() {
        int i = (transactionCount.get() == null ? 0 : transactionCount.get()) + 1;
        transactionCount.set(i);
        return i;
    }
}
